$(function(){
	$("#advertisement_list").datagrid({
		url:"advertisement_data.php",
		fit : true,
		fitColumns : true,
		striped : true,
		rownumbers : true,
		border : false,
		pagination : true,
		pageSize : 14,
		pageList : [14, 28, 42, 56, 70],
		pageNumber : 1,
		toolbar:'advertisement_tool',
		columns:[[
			{
				field : 'id',
				title : '编号',
				width : 10,
				checkbox : true,
			},
			{
				field : 'content',
				title : '广告内容',
				width : 100,
			},
			{
				field : 'date',
				title : '发布时间',
				width : 50,
			},
		]],
	});
	advertisement_tool ={
		add:function(){
			$("#add_advertisement").dialog("open");
			$("#content").focus();
		},
		remove : function () {
			var rows = $('#advertisement_list').datagrid('getSelections');
			if (rows.length > 0) {
				$.messager.confirm('确定操作', '您正在要删除所选的记录吗？', function (flag) {
					if (flag) {
						var ids = [];
						for (var i = 0; i < rows.length; i ++) {
							ids.push(rows[i].id);
						}
						$.ajax({
							type : 'POST',
							url : 'deleteAdvertisement.php',
							data : {
								ids : ids.join(','),
							},
							beforeSend : function () {
								$('#advertisement_list').datagrid('loading');
							},
							success : function (data) {
								if (data) {
									$('#advertisement_list').datagrid('loaded');
									$('#advertisement_list').datagrid('load');
									$('#advertisement_list').datagrid('unselectAll');
									$.messager.show({
										title : '提示',
										msg : data + '条广告被删除成功！',
									});
								}
							},
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要删除的广告！', 'info');
			}
		},
		edit:function(){
			var rows = $("#advertisement_list").datagrid("getSelections");
			if(rows.length>1){
				$.messager.alert("警告操作","每次修改只能选择一条广告",'warning');
			}else if(rows.length==0){
				$.messager.alert("警告操作","请选择要修改的广告",'warning');
			}else{
				$.ajax({
					url : 'getAdvertisement.php',
					type : 'post',
					data : {
						id : rows[0].id,
					},
					beforeSend : function () {
						$.messager.progress({
							text : '正在获取中...',
						});
					},
					success : function (data, response, status) {
						$.messager.progress('close');
						
						if (data) {
							var obj = $.parseJSON(data);
							$('#edit_advertisement').form('load', {
								advertisement_edit_id: obj[0].id,
								advertisement_edit_old: obj[0].content,
							}).dialog('open');
						}
					}
				});
			}
		},
	};
	$("#edit_advertisement").dialog({
		title:"修改广告",
		width:400,
		modal:true,
		closed:true,
		buttons:[{
			text:"提交",
			handler:function(){
				if ($('#edit_advertisement').form('validate')) {
					$.ajax({
						url : 'editAdvertisement.php',
						type : 'post',
						data : {
							id : $('input[name="advertisement_edit_id"]').val(),
							content : $('input[name="advertisement_edit_new"]').val(),
							time : getNowFormatDate(),
						},
						beforeSend : function () {
							$.messager.progress({
								text : '正在修改中...',
							});
						},
						success : function (data, response, status) {
							$.messager.progress('close');
							console.log(data);
							if (data > 0) {
								$.messager.show({
									title : '提示',
									msg : '修改广告成功',
								});
								$('#edit_advertisement').dialog('close').form('reset');
								$('#advertisement_list').datagrid('reload');
							} else {
								$.messager.alert('修改失败！', '未知错误导致失败，请重试！', 'warning');
							}
						}
					});
				}
			},
		},{
			text:"取消",
			handler:function(){
				$("#edit_advertisement").dialog("close").form("reset");
			},
		}],
	});
	function getNowFormatDate() {
		var date = new Date();
		var seperator1 = "-";
		var seperator2 = ":";
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
			+ " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
		return currentdate;
	}
	$("#add_advertisement").dialog({
		title:"新增广告",
		width:400,
		modal:true,
		closed:true,
		buttons:[{
			text:"提交",
			handler:function(){
				if ($('#add_advertisement').form('validate')) {
					$.ajax({
						url : 'addAdvertisement.php',
						type : 'post',
						data : {
							content : $('input[name="content"]').val(),
							time : getNowFormatDate(),
						},
						beforeSend : function () {
							$.messager.progress({
								text : '正在新增中...',
							});
						},
						success : function (data, response, status) {
							$.messager.progress('close');
							if (data) {
								$.messager.show({
									title : '提示',
									msg : '新增广告成功',
								});
								$('#add_advertisement').dialog('close').form('reset');
								$('#advertisement_list').datagrid('reload');
							} else {
								$.messager.alert('新增失败！', '广告发布失败，请重试！', 'warning');
							}
						}
					});
				}
			},
		},{
			text:"取消",
			handler:function(){
				$("#add_advertisement").dialog("close").form("reset");
			},
		}],
	});
	//广告验证
	$('input[name="content"]').validatebox({
		required : true,
		missingMessage : '请输入广告内容',
	});
});